package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.mapper.SysDicChildMapper;
import com.doubi.model.sys.TSysDicChild;
import com.doubi.model.sys.TSysUser;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysDicChildService extends ServiceImpl<SysDicChildMapper, TSysDicChild> {
    @Autowired
    private SysDicChildMapper sysDicChildMapper;


    /**
     * 功能描述：根据parentOid删除所有的子集
     * @author   MrDoubi 2019-06-28 15:48
     * @param parentOid     父级oid
     * @return              返回删除数量
     */
    public Integer deleteAllByParentOid(String parentOid) {
        if (DataUtil.isNotEmpty(parentOid)) {
            return sysDicChildMapper.deleteAllByParentOid(parentOid);
        } else {
            return 0;
        }
    }

    /**
     * 功能描述：根据parentOid获取List<TSysDicChild>
     * @author   MrDoubi 2019-06-28 17:26
     * @param parentOid         父级oid
     * @return                  返回List<TSysDicChild>对象
     */
    public List<TSysDicChild> getAllByParentOid(String parentOid) {
        QueryWrapper<TSysDicChild> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_oid",parentOid);
        return sysDicChildMapper.selectList(wrapper);
    }

    /**
     * 功能描述：更新字典表子集状态
     * @author   MrDoubi 2019-06-29 09:06
     * @param sysDicChild       TSysDicChild对象
     * @param sysUser           当前登录的TSysUser对象
     * @return                  返回AppModel对象
     */
    public AppModel updateStatus(TSysDicChild sysDicChild, TSysUser sysUser) {
        sysDicChild = sysDicChildMapper.selectById(sysDicChild.getOid());
        if(DataUtil.isEmpty(sysDicChild)){
            return new AppModel(-1,"参数错误！");
        }
        sysDicChild.setUpdateBy(sysUser.getOid());
        sysDicChild.setUpdateTime(new Date());
        if(sysDicChild.getStatus() == 1){
            sysDicChild.setStatus(2);
            sysDicChildMapper.updateById(sysDicChild);
            return new AppModel(0,"禁用成功！");
        }else {
            sysDicChild.setStatus(1);
            sysDicChildMapper.updateById(sysDicChild);
            return new AppModel(0,"启用成功！");
        }
    }

    /**
     * 功能描述：删除字典表子集
     * @author   MrDoubi 2019-06-29 10:08
     * @param sysDicChild       TSysDicChild对象
     * @return                  返回AppModel对象
     */
    public AppModel delBean(TSysDicChild sysDicChild) {
        sysDicChild = sysDicChildMapper.selectById(sysDicChild.getOid());
        if (DataUtil.isEmpty(sysDicChild)) {
            return new AppModel(-1, "参数错误！");
        } else {
            sysDicChildMapper.deleteById(sysDicChild.getOid());
            return new AppModel(0, "删除成功！");
        }
    }

    /**
     * 功能描述：保存或更新字典表子集
     * @author   MrDoubi 2019-06-29 10:11
     * @param sysDicChild       TSysDicChild对象
     * @param sysUser           当前登录的TSysUser对象
     * @return                  返回AppModel对象
     */
    public AppModel saveOrUpdate(TSysDicChild sysDicChild, TSysUser sysUser) {
        if(DataUtil.isNotEmpty(sysDicChild.getOid())){
            TSysDicChild bean = sysDicChildMapper.selectById(sysDicChild.getOid());
            bean.setItemCode(sysDicChild.getItemCode());
            bean.setItemName(sysDicChild.getItemName());
            bean.setStatus(sysDicChild.getStatus());
            bean.setOrderBy(sysDicChild.getOrderBy());
            bean.setRemark(sysDicChild.getRemark());
            bean.setUpdateBy(sysUser.getOid());
            bean.setUpdateTime(new Date());
            sysDicChildMapper.updateById(bean);
            return new AppModel(0,"修改成功！");
        }else {
            sysDicChild.setCreateBy(sysUser.getOid());
            sysDicChild.setCreateTime(new Date());
            sysDicChildMapper.insert(sysDicChild);
            return new AppModel(0,"添加成功！");
        }
    }
}
